<template>
  <div style="height:100%">
    <el-form ref="form" :model="form" :rules="rules" :disabled="!form.busPageInfo.editable" size="mini" label-width="160px">
      <el-row :gutter="2">
        <el-col :span="8">
          <el-form-item label="组织机构" prop="busMainData.sysOrgCode">
            <OrgsetTreeSelect v-model="form.busMainData.sysOrgCode" :editable="form.busPageInfo.editable && form.busMainData.funProcess === '10'" :has-priv="true" style="width: 200px" />
          </el-form-item>
          <el-form-item label="业务时间" prop="busMainData.busDate">
            <el-date-picker
              v-model="form.busMainData.busDate"
              placeholder="业务时间"
              type="datetime"
              size="mini"
              clearable
              style="width: 200px"
              class="filter-item" />
          </el-form-item>
          <el-form-item label="BOM编码" prop="busMainData.bomCode">
            <base-bom-select-dialog v-model="form.busMainData.bomCode" :editable="form.busPageInfo.editable && form.busMainData.funProcess === '10'" width="200px" @bomSelectCallback="bomSelectCallback"/>
            <el-input :disabled="true" v-model="form.busMainData.prodItemCode" style="width:100px;" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="供应商" prop="busMainData.otherSysOwnerCpy">
            <CpyOrgSetSelectDialog
              v-model="form.busMainData.busOtherCpyName"
              :editable="form.busPageInfo.editable && form.busMainData.funProcess === '10'"
              :cpy-code="form.busMainData.otherSysOwnerCpy"
              :ori-org-code="form.busMainData.otherSysOrgCode"
              cpy-owner-type="supply"
              @cpySelectCallback="cpySelectCallback"
              @orgSetSelectCallback="orgSetSelectCallback" />
          </el-form-item>
          <el-form-item label="订单号" prop="busMainData.orderCode">
            <order-item-select-dialog v-model="form.busMainData.orderCode" :editable="form.busPageInfo.editable && form.busMainData.funProcess === '10'" :param-data="{'itemId': form.busMainData.prodItemId }" @orderItemSelectCallback="orderItemSelectCallback"/>
          </el-form-item>
          <el-form-item label="外发物料/数量" prop="busMainData.itemId">
            <BaseItemSelectDialog v-model="form.busMainData.itemCode" :editable="form.busPageInfo.editable && form.busMainData.funProcess === '10'" width="180px" @itemSelectCallback="itemSelectCallback" />
            <el-input-number v-model.number="form.busMainData.prodQty" :controls="false" style="width: 120px;" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="预计开工/结束日期" prop="busMainData.scheduleBeginDate">
            <el-date-picker
              v-model="form.busMainData.scheduleBeginDate"
              placeholder="预计开工日期"
              type="date"
              size="mini"
              clearable
              style="width: 140px"
              class="filter-item" /> -
            <el-date-picker
              v-model="form.busMainData.scheduleEndDate"
              placeholder="预计结束日期"
              type="date"
              size="mini"
              clearable
              style="width: 140px"
              class="filter-item" />
          </el-form-item>
          <el-form-item label="实际开工/结束日期" prop="busMainData.actualBeginDate">
            <el-date-picker
              v-model="form.busMainData.actualBeginDate"
              placeholder="实际开工日期"
              type="date"
              size="mini"
              clearable
              style="width: 140px"
              class="filter-item" />-
            <el-date-picker
              v-model="form.busMainData.actualEndDate"
              placeholder="预计结束日期"
              type="date"
              size="mini"
              clearable
              style="width: 140px"
              class="filter-item" />
          </el-form-item>
          <el-form-item label="备注" prop="busMainData.busRmk">
            <el-input
              v-model="form.busMainData.busRmk"
              class="filter-item" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <mo-detail ref="moDetail" :sup-form="thisForm" :mo-details="form.moDetails" :editable="form.busPageInfo.editable" @setDetails="setDetails" />
    <div v-if="form.busPageInfo.editable" slot="footer" class="modal-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button v-permission="['SYS_ADMIN','COMMON_ADMIN','OEM_MO_ALL','OEM_MO_CHANGE','OEM_MO_CREATE']" :loading="loading" type="primary" @click="doSave">暂存</el-button>
      <el-button v-permission="['SYS_ADMIN','COMMON_ADMIN','OEM_MO_ALL','OEM_MO_SUBMIT']" :loading="loading" type="primary" @click="doSubmit">提交</el-button>
    </div>
  </div>
</template>

<script>
import emsMo from '@/api/prod/EmsMo'
import MoDetail from './moDetail'
import OrgsetTreeSelect from '@/views/components/OrgsetTreeSelect'
import BaseBomSelectDialog from '@/views/components/BaseBomSelectDialog'
import OrderItemSelectDialog from '@/views/components/OrderItemSelectDialog'
import BaseItemSelectDialog from '@/views/components/BaseItemSelectDialog'
import baseBom from '@/api/base/BaseBom'
import CpyOrgSetSelectDialog from '@/views/components/CpyOrgSetSelectDialog'

export default {
  components: { OrderItemSelectDialog, BaseBomSelectDialog, MoDetail, OrgsetTreeSelect, BaseItemSelectDialog, CpyOrgSetSelectDialog },
  dictTypes: ['mo_status'],
  props: {
    sup_this: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      isAdd: false,
      loading: false,
      thisForm: this,
      style: 'width: 184px',
      dialogInfo: {
        name: 'emsMoMngFormDialog',
        title: 'MO外发工单',
        fullScreen: true,
        width: 500,
        height: 90
      },
      form: {
        moDetails: [],
        busMainData: {
          id: '',
          enableFlag: 'Y'
        },
        busPageInfo: {
          editable: false
        }
      },
      rules: {
        'busMainData.busDate': [
          { required: true, message: '请选择业务时间', trigger: 'blur' }
        ],
        'busMainData.sysOrgCode': [
          { required: true, message: '请选择机构', trigger: 'blur' }
        ],
        'busMainData.itemId': [
          { required: true, message: '请选择生产物料', trigger: 'blur' }
        ],
        'busMainData.bomCode': [
          { required: true, message: '请选择BOM编号', trigger: 'blur' }
        ],
        'busMainData.otherSysOwnerCpy': [
          { required: true, message: '请选择供应商', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.$emit('afterMounted')
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    initDataOfBefore() {
    },
    viewApproveData(busId) {
      const data = { paramBean: { busId: busId }}
      emsMo.toViewApprove(data).then(res => {
        this.form = res.data
        this.getOtherDetailData(this.form.busMainData.id, false, false)
      })
    },
    getOtherDetailData(mainId, replaceFlag, replaceDetailLotFlag) {
      if (mainId) {
        emsMo.getDetailData(mainId, replaceFlag, replaceDetailLotFlag).then(res => {
          this.form.moDetails = res.data
        })
      }
    },
    addData() {
      this.isAdd = true
      const data = { paramBean: {}}
      emsMo.toAdd(data).then(res => {
        this.displayDialog()
        this.form = res.data
        this.getOtherDetailData(null, true, true)
      }).catch(err => {
        console.log(err)
      })
    },
    copyData(mainId) {
      this.isAdd = true
      const data = { paramBean: { id: mainId }}
      emsMo.toCopy(data).then(res => {
        this.displayDialog()
        this.form = res.data
        this.getOtherDetailData(null, true, true)
      }).catch(err => {
        console.log(err)
      })
    },
    viewMainData(id) {
      this.isAdd = false
      const data = { paramBean: { id: id }}
      emsMo.viewMainData(data).then(res => {
        this.displayDialog()
        this.form = res.data
        this.getOtherDetailData(id, false, false)
      })
    },
    doSubmit() {
      this.doSingleSave(true)
    },
    doSave() {
      this.doSingleSave(false)
    },
    doSingleSave(isSubmit) {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form.formSubmit = isSubmit
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else {
            this.doEdit()
          }
        }
      })
    },
    doAdd() {
      emsMo.addOrEdit(this.form).then(res => {
        this.resetForm()
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    doEdit() {
      emsMo.addOrEdit(this.form).then(res => {
        this.resetForm()
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    resetForm() {
      this.hiddenDialog()
      this.$refs['form'].resetFields()
    },
    changeData(busId) {
      const data = { paramBean: { id: busId }}
      emsMo.toChange(data).then(res => {
        this.displayDialog()
        this.form = res.data
        this.getOtherDetailData(this.form.busMainData.oldMainId, true, false)
      }).catch(err => {
        this.dialog = false
        console.log(err)
      })
    },
    itemSelectCallback(selectedRow, selectObj) {
      this.form.busMainData.itemId = ''
      this.form.busMainData.itemCode = ''
      if (selectedRow) {
        this.form.busMainData.itemId = selectedRow.id
        this.form.busMainData.itemCode = selectedRow.itemCode
      }
    },
    setDetails(details) {
      this.form.moDetails = details
    },
    bomSelectCallback(selectedRow, selectObj, paramData) {
      const oldItemCode = this.form.busMainData.prodItemCode
      this.form.busMainData.prodItemCode = null
      this.form.busMainData.prodItemId = null
      if (selectedRow) {
        this.form.busMainData.bomId = selectedRow.id
        this.form.busMainData.bomCode = selectedRow.sysGenCode
        this.form.busMainData.prodItemCode = selectedRow.itemCode
        this.form.busMainData.prodItemId = selectedRow.itemId
        if (oldItemCode !== selectedRow.itemCode) {
          //  编号不一致，则清空
          this.form.busMainData.orderDetailLotNo = null
          this.form.busMainData.orderCode = null
        }
      }
    },
    orderItemSelectCallback(selectedRow, paramData) {
      const oldItemCode = this.form.busMainData.prodItemCode
      this.form.busMainData.prodItemCode = null
      this.form.busMainData.prodItemId = null
      if (selectedRow) {
        this.form.busMainData.orderDetailLotNo = selectedRow.detailLotNo
        this.form.busMainData.orderCode = selectedRow.sysGenCode
        this.form.busMainData.prodItemCode = selectedRow.itemCode
        this.form.busMainData.prodItemId = selectedRow.itemId
        if (oldItemCode !== selectedRow.itemCode) {
          //  编号不一致，则清空
          this.form.busMainData.bomId = null
          this.form.busMainData.bomCode = null
        }
        if (!this.form.busMainData.bomCode) {
          // 没有选择BOM, 则系统自动选择 BOM编号
          baseBom.getBomInfoByItemId(selectedRow.itemId).then(res => {
            const bomData = res.data
            if (bomData) {
              this.form.busMainData.bomId = bomData.id
              this.form.busMainData.bomCode = bomData.sysGenCode
            }
          })
        }
      }
    },
    cpySelectCallback(selectedRow, selectObj) {
      this.form.busMainData.otherSysOwnerCpy = ''
      this.form.busMainData.busOtherCpyName = ''
      if (selectedRow) {
        this.form.busMainData.otherSysOwnerCpy = selectedRow.cpyCode
        this.form.busMainData.busOtherCpyName = selectedRow.cpyLongName
      }
      selectObj.cancel()
    },
    orgSetSelectCallback(orgCode) {
      this.form.busMainData.otherSysOrgCode = orgCode
    }
  }
}
</script>
